// pages/workReport/index.js
const {
  get,
  post
} = require('../../request/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemInfo: {},
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    equipmentList: [{
      part: "",
      model: "",
      unit: "",
      num: "",
      manufacturer: "",
    }],
    parmes: {
      depatName: "",
      equCode: "",
      equName: "",
      Content: "",
      repairId: '',
      userSign: "",
      userId: '',
      repairName: ''
    },
    show: false,
    showPeople: false,
    wuanlist: [], // 人员数据
    wuanlistCache: [], // 模糊搜索数据
    peopleValue: '', // 模糊搜索数据关键词

  },
  onChangePeople(name) {
    let newListData = []; // 用于存放搜索出来数据的新数组
    if (name.detail) {
      this.data.wuanlist.forEach((item) => {
        if (item.nickName.indexOf(name.detail) !== -1) {
          console.log(item);
          newListData.push(item);
        }
      });
    }
    this.setData({
      wuanlistCache: newListData
    })
  },
  async postbalstWork() {
    let res = await get(`/system/user/list?pageNum=1&pageSize=999`)
    this.setData({
      wuanlist: res.rows
    })
  },
  peopleBtn(name) {
    this.setData({
      showPeople: true,
      formName: name.target.dataset.name
    })
  },
  onClosePeople() {
    this.setData({
      showPeople: false
    })
  },

  toggle(event) {
    console.log(event);
    const {
      name,
      text
    } = event.currentTarget.dataset;
    let value = 'parmes.repairId'
    let label = 'parmes.repairName'

    this.setData({
      showPeople: false,
      [label]: text,
      [value]: name
    });
  },
  // 新增零件信息
  add() {
    let list = this.data.equipmentList
    list.push({
      part: "",
      model: "",
      unit: "",
      num: "",
      manufacturer: "",
    })
    this.setData({
      equipmentList: list
    })
  },
  minus(e) {
    console.log(e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index
    let list = this.data.equipmentList
    list.splice(index, 1)
    this.setData({
      equipmentList: list
    })
  },
  onChangeInput(e) {
    this.setData({
      [e.target.dataset.lable]: e.detail
    })
  },
  onChangeInputList(e) {
    let index = e.target.dataset.index
    let label = e.target.dataset.lable
    let value = 'equipmentList[' + index + '].' + label
    this.setData({
      [value]: e.detail
    })
  },
  canvasChang(e) {
    let value = 'parmes.userSign'
    this.setData({
      [value]: e.detail
    })
  },
  submit() {
    let obj = Object.assign(this.data.parmes, {
      userId: this.data.prolist.userId,
      equipmentPartList: this.data.equipmentList
    })
    console.log(obj);
    post('/app/equipment', obj).then(res => {
      if (res.code == 200) {
        const pages = getCurrentPages() //获取页面列表
        const perpage = pages[pages.length - 2] //上一页   
        perpage.onLoad()
        wx.showToast({
          title: '提交成功',
          duration: 3000,
          icon: 'success',
          mask: true
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 200)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.postbalstWork()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})